package com.android.tiango.baba.act;

import java.io.File;
import java.util.ArrayList;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.LoginEntity;
import com.android.tiango.baba.entity.LogisticEnity;
import com.android.tiango.baba.entity.LogisticsInformationModel;
import com.android.tiango.baba.entity.OrderModel;
import com.android.tiango.baba.entity.UserModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

public class ActLogisticList extends BaListActivity<LogisticsInformationModel> implements PullListViewListener{

	private LayoutInflater mInflater;
	private String mExpressNO;//订单号
	private OrderModel mOderModel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		initView();
	}
	private void initView() {
		// TODO Auto-generated method stub
		setTitleName("物流详情");
		mTitleBar.setBackIcon(R.drawable.back);
		setBackOnClickListener();
		
		mOderModel=(OrderModel) getIntent().getSerializableExtra(Constants.DATA);
		mExpressNO=mOderModel.getExpressNO();
		
		mInflater=getLayoutInflater();
		View view=mInflater.inflate(R.layout.header_logistic, null);
		
		setheadView(view);
		
		mListView.setDivider(null);
		mListView.addHeaderView(view);
		//mListView.startOnRefresh();
		
	}
	private void setheadView(View view) {
		// TODO Auto-generated method stub
		if(null!=mOderModel){
			
			
			String name=mOderModel.getSimpleProduct().getProductName();
			String url=mOderModel.getSimpleProduct().getProductImage().getImageUrl();
			String merchant=mOderModel.getSimpleProduct().getMerchant();
			String creatTime=TimeToUtil.getTimeLogisticFormat(Long.valueOf(mOderModel.getCreateTime()));
			
			
			Uri Imageuri=Uri.fromFile(new File(url));
			
			ImageView baobei=(ImageView) view.findViewById(R.id.iv_baobei);
			TextView Name=(TextView) view.findViewById(R.id.tv_name);
			TextView companyName=(TextView) view.findViewById(R.id.tv_company_name);
			TextView date=(TextView) view.findViewById(R.id.tv_data);
			
			companyName.setText(merchant);
			Name.setText(name);
			baobei.setImageURI(Uri.fromFile(new File(url)));
			date.setText(creatTime);
		}
	}
	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		viewHolder viewholder=null;
		itemAdapter itemadapter;
		if(convertView==null){
			convertView=mInflater.inflate(R.layout.item_logistics, null);
			viewholder=new viewHolder();
			viewholder.mDate=(TextView) convertView.findViewById(R.id.tv_date);
			viewholder.mItemListView=(ListView) convertView.findViewById(R.id.mybaListView1);
			viewholder.mLine=(ImageView) convertView.findViewById(R.id.iv_line);
			viewholder.mToday=(TextView) convertView.findViewById(R.id.tv_today);
			convertView.setTag(viewholder);
		}else{
			viewholder=(viewHolder) convertView.getTag();
		}
		LogisticsInformationModel bean=mAdapter.getItem(position);
		long time=Long.valueOf(bean.getLogTime());
		String message=bean.getLogDescription();
		
		if(position==0){
			viewholder.mDate.setText("");
			viewholder.mDate.setBackgroundResource(R.drawable.clock);
			viewholder.mLine.setVisibility(View.GONE);
			viewholder.mToday.setVisibility(View.VISIBLE);
			itemadapter=new itemAdapter(1,position);
		}else{
			viewholder.mDate.setText("9-30");
			viewholder.mDate.setBackgroundResource(R.drawable.gray_circle);
			viewholder.mLine.setVisibility(View.VISIBLE);
			viewholder.mToday.setVisibility(View.GONE);
			itemadapter=new itemAdapter(2,position);
		}
		
		
		
		viewholder.mItemListView.setAdapter(itemadapter);
		viewholder.mItemListView.setDivider(null);
		return convertView;
	}
	class viewHolder{
		private TextView mDate,mToday;
		private ListView mItemListView;
		private ImageView mLine;
	}
	class itemAdapter extends BaseAdapter{

		private int mCount;
		private int where=-1;
		@Override
		public int getCount() {
			return mCount;
		}
		public itemAdapter(int count,int position){
			mCount=count;
			where=position;
		}
		@Override
		public Object getItem(int position) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			if(position%2==0){
				view=mInflater.inflate(R.layout.item_logistics2, null);
			}else{
				view=mInflater.inflate(R.layout.item_logistics3, null);
			}
			
			TextView message=(TextView) view.findViewById(R.id.tv_message);
			TextView time=(TextView) view.findViewById(R.id.tv_time);
			ImageView point=(ImageView) view.findViewById(R.id.iv_point);
			if(where==0){
				message.setText("快件已签收");
				message.setTextColor(getResources().getColor(R.color.color_blue));
				time.setText("6:00");
				time.setTextColor(getResources().getColor(R.color.color_blue));
				point.setImageResource(R.drawable.point);
				
			}else{
				message.setText("快件在成都集散中心");
				message.setTextColor(Color.GRAY);
				time.setText("8:00");
				time.setTextColor(Color.GRAY);
				point.setImageResource(R.drawable.gray_point);
			}
			
			return view;
		}
		
	}
	private void loadDate() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());
		params.put("expressNO", mExpressNO);
		client.get(HttpUrl.getUrl(HttpUrl.LOGISTIC), params, new HttpCallBack() {
			@Override
			public void onHttpSuccess(Object ob) {
				System.out.println("ob :"+ob);
				dismissWaitingDialog();
				LogisticEnity logisticEnity=(LogisticEnity) ob;
				ArrayList<LogisticsInformationModel> list=logisticEnity.getData();
				if(null!=logisticEnity){
					mAdapter.putData(list);
				}
			}
			@Override
			public void onHttpStarted() {
				showWaitingDialog("正在加载...");
			}
			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				dismissWaitingDialog();
				if(mPageIndex>1){
					mPageIndex-=1;
				}
				if(!StringUtils.isEmpty(arg1)){
					ToastUtils.showMessage(arg1);
				}else{
					ToastUtils.showMessage("抱歉,请重新尝试");
					
				}
			}
		}, LogisticEnity.class);
	}
	@Override
	public void onPullRefresh() {
		// TODO Auto-generated method stub
		mPageIndex=1;
		loadDate();
	}
	
	@Override
	public void onPullLoadMore() {
		// TODO Auto-generated method stub
		mPageIndex+=1;
		loadDate();
	}
}
